﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using AnimalCare.Model;
using AnimalCare.Service;

namespace AnimalCare.View
{
    public partial class FormSupplier : DevExpress.XtraEditors.XtraForm
    {
        public FormSupplier()
        {
            InitializeComponent();
            refreshGrid();
            InitGrid();
        }


        void InitGrid()
        {
        }
        protected void refreshGrid()
        {
            AnimalCareDBEntities entities;
            entities = new AnimalCareDBEntities();
            gridSupplier.DataSource = entities.Suppliers;
            gridSupplier.Refresh();
        }

        private void InsertSupplier_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(textNameSupplier.Text))
            {
                SupplierService.Insert(textNameSupplier.Text, textphoneSupplier.Text, textInfoSupplier.Text);
                refreshGrid();
                textInfoSupplier.Text = "";
                textNameSupplier.Text = "";
                textphoneSupplier.Text = "";
            }
            else
            {
                ValidateService.validateEmpty(textNameSupplier, errorProvider1, "Chưa nhập tên nhà cung cấp");
            }
        }

        private void DeleteMenuItem_Click_1(object sender, EventArgs e)
        {
            int[] selectedRowsIndex = gridViewSupplier.GetSelectedRows();
            if (selectedRowsIndex.Length > 0)
            {
                int indexRow = selectedRowsIndex[0];
                Supplier cl = (Supplier)gridViewSupplier.GetRow(indexRow);
                SupplierService.Delete(cl.supplier_id);
                refreshGrid();
            }
        }

        private void UpdateMenuItem_Click_1(object sender, EventArgs e)
        {
            int[] selectedRowsIndex = gridViewSupplier.GetSelectedRows();
            if (selectedRowsIndex.Length > 0)
            {
                int indexRow = selectedRowsIndex[0];
                Supplier cl = (Supplier)gridViewSupplier.GetRow(indexRow);
                SupplierService.Update(cl);
                refreshGrid();
            }
        }
    }
}
